package com.haidechizi.i18n.code.generator.common.message.translate;

import com.github.stuxuhai.jpinyin.ChineseHelper;
import com.haidechizi.i18n.code.generator.common.message.constants.LanguageKeyConstants;

import javax.annotation.PostConstruct;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 抽象类，用于保存翻译类
 */
public abstract class AbstractTranslate implements Translate {

    private static final Map<String, Translate> translateMap = new ConcurrentHashMap<>(8);

    @PostConstruct
    public void init() {
        translateMap.put(type(), this);
    }

    public static final Translate getInstance(String type) {
        return translateMap.get(type);
    }

    /**
     * 翻译顶层接口
     * <p>
     * zh_CN->language
     *
     * @param data
     * @param language
     * @return
     */
    @Override
    public String translate(String data, String language) {
        data = ChineseHelper.convertToSimplifiedChinese(data);
        return translate(data, LanguageKeyConstants.ZH_CN, language);
    }
}
